<?php
/**
 * Created by PhpStorm.
 * User: chenhaibin
 * Date: 17/8/1
 * Time: 下午11:18
 */
namespace app\mobile\controller;

use app\home\logic\UsersLogic;
use app\home\model\Message;
use app\common\logic\OrderLogic;
use think\Page;
use think\Request;
use think\Verify;
use think\db;

class Distribut extends MobileBase
{
    public $user_id = 0;
    public $user = array();

    /*
    * 初始化操作
    */
    public function _initialize()
    {
        parent::_initialize();
        if (session('?user')) {
            $user = session('user');
            $user = M('users')->where("user_id", $user['user_id'])->find();
            session('user', $user);  //覆盖session 中的 user
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息
        }
        $nologin = array(
            'login'
        );
        if (!$this->user_id && !in_array(ACTION_NAME, $nologin)) {
            header("location:" . U('Mobile/User/login'));
            exit;
        }

    }

    public function index()
    {
        return $this->fetch();
    }

//    分销的订单
    public function order_list()
    {
        $status = I('status','0');

        $where = " status in ($status) and user_id = $this->user_id  ";

        $count = M('rebate_log')->where($where)->count();
        $Page = new Page($count, 10);
        $show = $Page->show();

        $all_order_info = M('rebate_log')->alias('o')
            ->field('o.order_id,o.order_sn,o.nickname,o.status,o.create_time')
            ->join('__USERS__ u', 'o.user_id = u.user_id')
            ->where('o.user_id', $this->user_id)
            ->where('o.status','in',$status)
            ->order('o.create_time desc')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();

//        echo M('rebate_log')->getlastsql();

        foreach($all_order_info as $key=>$val){
            $all_order_info[$key]['goods_list'] = M('order_goods')->where('order_id',$val['order_id'])->select();
        }

//        echo M('order')->getlastsql();
//        dump($all_order_info);
        $this->assign('list', $all_order_info);

        if ($_GET['is_ajax']) {
            return $this->fetch('ajax_disorder_list');
            exit;
        }
        return $this->fetch();
    }

    //    我的分销团队
    public function lower_list()
    {
        $lists =  M('users')->where('first_leader',$this->user_id)->order('reg_time','desc')->select();

        $this->assign('lists', $lists);
        return $this->fetch();
    }

    //    我的二维码
    public function qr_code()
    {
        return $this->fetch();
    }
    //    分销排行
    public function rankings()
    {
        return $this->fetch();
    }
    //    分销必看
    public function must_see()
    {
        return $this->fetch();
    }
    //    我的网店
    public function my_store()
    {
        return $this->fetch();
    }
    //    佣金明细
    public function rebate_log()
    {
        return $this->fetch();
    }
    //    设置店铺
    public function set_store()
    {
        return $this->fetch();
    }
}